package com.example.service;

import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Speciality;
import com.example.exception.CustomException;
import com.example.mapper.SpecialityMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 专业信息表业务处理
 **/
@Service
public class SpecialityService {

    @Resource
    private SpecialityMapper specialityMapper;

    /**
     * 新增
     * 权限控制：只有管理员可以新增专业
     */
    public void add(Speciality speciality) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && !RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            throw new CustomException(ResultCodeEnum.NO_AUTH);
        }
        specialityMapper.insert(speciality);
    }

    /**
     * 删除
     * 权限控制：只有管理员可以删除专业
     */
    public void deleteById(Integer id) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && !RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            throw new CustomException(ResultCodeEnum.NO_AUTH);
        }
        specialityMapper.deleteById(id);
    }

    /**
     * 批量删除
     * 权限控制：只有管理员可以批量删除专业
     */
    public void deleteBatch(List<Integer> ids) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && !RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            throw new CustomException(ResultCodeEnum.NO_AUTH);
        }
        // 使用 Java 21 增强的 Stream API
        ids.forEach(specialityMapper::deleteById);
    }
    /**
     * 修改
     * 权限控制：只有管理员可以修改专业
     */
    public void updateById(Speciality speciality) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && !RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            throw new CustomException(ResultCodeEnum.NO_AUTH);
        }
        specialityMapper.updateById(speciality);
    }

    /**
     * 根据ID查询
     */
    public Speciality selectById(Integer id) {
        return specialityMapper.selectById(id);
    }

    /**
     * 查询所有
     * 权限控制：教师只能查看自己学院的专业
     */
    public List<Speciality> selectAll(Speciality speciality) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && RoleEnum.TEACHER.name().equals(currentUser.getRole())) {
            // 如果是教师，需要先获取教师的学院信息
            com.example.entity.Teacher teacher = (com.example.entity.Teacher) currentUser;
            if (teacher.getCollegeId() != null) {
                // 只返回教师所在学院的专业
                speciality.setCollegeId(teacher.getCollegeId());
            }
        }
        return specialityMapper.selectAll(speciality);
    }

    /**
     * 分页查询
     * 权限控制：教师只能查看自己学院的专业
     */
    public PageInfo<Speciality> selectPage(Speciality speciality, Integer pageNum, Integer pageSize) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && RoleEnum.TEACHER.name().equals(currentUser.getRole())) {
            // 如果是教师，需要先获取教师的学院信息
            com.example.entity.Teacher teacher = (com.example.entity.Teacher) currentUser;
            if (teacher.getCollegeId() != null) {
                // 只返回教师所在学院的专业
                speciality.setCollegeId(teacher.getCollegeId());
            }
        }
        
        PageHelper.startPage(pageNum, pageSize);
        List<Speciality> list = specialityMapper.selectAll(speciality);
        return PageInfo.of(list);
    }

}
